package com.lobo.blog.utils;

import org.commonmark.Extension;
import org.commonmark.ext.gfm.tables.TableBlock;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
import org.commonmark.node.Link;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.AttributeProvider;
import org.commonmark.renderer.html.AttributeProviderContext;
import org.commonmark.renderer.html.AttributeProviderFactory;
import org.commonmark.renderer.html.HtmlRenderer;

import java.util.*;

/**
 * className: MarkDownToHtmlTool<br/>
 * author: MacieSerenity <br/>
 * date: 2022-10-14 13:57
 **/
public class MarkDownToHtmlTool {
    /**
     * markdown格式转换成HTML格式
     *
     * @param markdown
     * @return
     */
    /**
     * markdown格式转换成HTML格式
     *
     * @param markdown
     * @return
     */
    public static String markdownToHtml(String markdown) {
        Parser parser = Parser.builder().build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        return renderer.render(document);
    }

    /**
     * 增加扩展[标题锚点，表格生成]
     * Markdown转换成HTML
     *
     * @param markdown
     * @return
     */
    public static String markdownToHtmlExtensions(String markdown) {
        //h标题生成id
        Set<Extension> headingAnchorExtensions = Collections.singleton(HeadingAnchorExtension.create());
        //转换table的HTML
        List<Extension> tableExtension = Arrays.asList(TablesExtension.create());
        Parser parser = Parser.builder()
                .extensions(tableExtension)
                .build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder()
                .extensions(headingAnchorExtensions)
                .extensions(tableExtension)
                .attributeProviderFactory(new AttributeProviderFactory() {
                    @Override
                    public AttributeProvider create(AttributeProviderContext context) {
                        return new CustomAttributeProvider();
                    }
                })
                .build();
        return renderer.render(document);
    }

    /**
     * 处理标签的属性
     */
    static class CustomAttributeProvider implements AttributeProvider {
        @Override
        public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
            //改变a标签的target属性为_blank
            if (node instanceof Link) {
                attributes.put("target", "_blank");
            }
            if (node instanceof TableBlock) {
                attributes.put("class", "ui celled table");
            }
        }
    }


    public static void main(String[] args) {
        String table = "| hello | hi   | 哈哈哈   |\n" +
                "| ----- | ---- | ----- |\n" +
                "| 斯维尔多  | 士大夫  | f啊    |\n" +
                "| 阿什顿发  | 非固定杆 | 撒阿什顿发 |\n" +
                "\n";
        String a = "```json\n" +
                "PageInfo{\n" +
                "pageNum=1, \n" +
                "pageSize=5,\n" +
                "size=5, \n" +
                "startRow=0, \n" +
                "endRow=4, \n" +
                "total=5, \n" +
                "pages=1, \n" +
                "list=[\n" +
                "    Type(id=1, name=Java, updateTime=Thu Sep 29 22:02:25 CST 2022, createTime=Thu Sep 29 22:02:25 CST 2022, blogs=null),\n" +
                "    Type(id=2, name=Python, updateTime=Thu Sep 29 22:20:13 CST 2022, createTime=Thu Sep 29 22:20:13 CST 2022, blogs=null),\n" +
                "    Type(id=3, name=C#, updateTime=Thu Sep 29 22:30:55 CST 2022, createTime=Thu Sep 29 22:30:55 CST 2022, blogs=null),\n" +
                "    Type(id=4, name=SpringBoot, updateTime=Fri Sep 30 11:08:29 CST 2022, createTime=Fri Sep 30 11:08:29 CST 2022, blogs=null), \n" +
                "    Type(id=5, name=Unity, updateTime=Fri Sep 30 15:51:10 CST 2022, createTime=Fri Sep 30 15:51:10 CST 2022, blogs=null)\n" +
                "],\n" +
                "prePage=0, \n" +
                "nextPage=0,\n" +
                "isFirstPage=true, \n" +
                "isLastPage=true,\n" +
                "hasPreviousPage=false,\n" +
                "hasNextPage=false,\n" +
                "navigatePages=8, \n" +
                "navigateFirstPage=1,\n" +
                "navigateLastPage=1, \n" +
                "navigatepageNums=[1]}\n" +
                "\n" +
                "```\n" +
                "\n" +
                "```\n" +
                "select type_id,count(type_id) as size from t_blog group by type_id order by size desc limit 6;\n" +
                "\n" +
                "\n" +
                "+---------+------+\n" +
                "| type_id | size |\n" +
                "+---------+------+\n" +
                "|      22 |   10 |\n" +
                "|      16 |    4 |\n" +
                "|      34 |    2 |\n" +
                "|      33 |    1 |\n" +
                "+---------+------+\n" +
                "4 rows in set (0.00 sec)\n" +
                "\n" +
                "\n" +
                "(select type_id as type_id,count(type_id) as size from t_blog group by type_id order by size desc limit 6);\n" +
                "select id,name,size from t_type t right join (select type_id as type_id,count(type_id) as size from t_blog group by type_id order by size desc limit 6) as temp on t.id=temp.type_id;\n" +
                "\n" +
                "select id,name,size from t_type t right join (select type_id as type_id,count(type_id) as size from t_blog group by type_id limit 6) as temp on t.id=temp.type_id where t.is_deleted = 0 order by size desc;\n" +
                "\n" +
                "\n" +
                "\n" +
                "```\n" +
                "\n" +
                "```\n" +
                "select id, name, size\n" +
                "from t_type t right join\n" +
                "(select type_id as type_id, count(type_id) as size from\n" +
                "t_blog where published=1 group by type_id limit 6)\n" +
                "as temp\n" +
                "on t.id=temp.type_id\n" +
                "where t.is_deleted = 0\n" +
                "order by size desc;\n" +
                "```\n" +
                "\n" +
                "```\n" +
                "PageInfo{pageNum=1, pageSize=10, size=3, startRow=1, endRow=3, total=3, pages=1, \n" +
                "list=Page{count=true, pageNum=1, pageSize=10, startRow=0, endRow=10, total=3, pages=1, reasonable=false, pageSizeZero=false}\n" +
                "[Tag(id=1, name=标签1, updateTime=Thu Oct 13 18:01:36 CST 2022, createTime=Thu Oct 13 18:01:39 CST 2022, isDeleted=0),\n" +
                "Tag(id=2, name=标签2, updateTime=Thu Oct 13 10:11:13 CST 2022, createTime=Thu Oct 13 10:11:13 CST 2022, isDeleted=0),\n" +
                "Tag(id=3, name=标签3, updateTime=Thu Oct 13 10:11:17 CST 2022, createTime=Thu Oct 13 10:11:17 CST 2022, isDeleted=0)],\n" +
                "prePage=0, nextPage=0, isFirstPage=true, isLastPage=true, hasPreviousPage=false, hasNextPage=false, navigatePages=8, navigateFirstPage=1, navigateLastPage=1, navigatepageNums=[1]}\n" +
                "\n" +
                "```\n" +
                "\n" +
                "```\n" +
                "Page{\n" +
                "count=true,\n" +
                "pageNum=1,\n" +
                "pageSize=10,\n" +
                "startRow=0,\n" +
                "endRow=10,\n" +
                "total=3,\n" +
                "pages=1, \n" +
                "reasonable=false, \n" +
                "pageSizeZero=false}\n" +
                "\n" +
                "\n" +
                "```\n" +
                "\n" +
                "```\n" +
                "select tag_id as tag_id,count(blog_id) as size from t_blog_tag group by tag_id;\n" +
                "\n" +
                "select id, name, update_time, create_time ,size from t_tag t left join (select tag_id as tag_id,count(blog_id) as size from t_blog_tag group by tag_id) as temp on temp.tag_id=t.id where is_deleted = 0 order by update_time desc limit 6;\n" +
                "```\n" +
                "\n";
        System.out.println(markdownToHtmlExtensions(a));
    }
}
